<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreateWaimaiOrderSpliteTables extends Migration
{
    private $name;

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $cFunc = function (Blueprint $table) {
            $table->unsignedBigInteger('orderId')->primary();
            $table->unsignedInteger('brandId')->default(0)->comment('品牌id');
            $table->unsignedInteger('shopId')->default(0)->comment('我方店铺id');
            $table->unsignedTinyInteger('sourceType')->default(0)->comment('来源 3美团 4饿了么');
            $table->integer("serialNum")->default(0)->comment("流水号");
            $table->unsignedTinyInteger('orderStatus')->default(0)->comment('订单状态');

            $table->integer("originalPrice")->default(0)->comment("营业额+原配送费+包装费");
            $table->integer("payPrice")->default(0)->comment("实付");
            $table->integer("discountPrice")->default(0)->comment("折扣金额");
            $table->unsignedInteger('shippingPrice')->default(0)->comment('实付的配送费');
            $table->unsignedInteger('originalShippingPrice')->default(0)->comment('原配送费');
            $table->unsignedInteger('packingPrice')->default(0)->comment('包装费');
            $table->unsignedInteger('platformPrice')->default(0)->comment('平台服务费');
            $table->unsignedInteger('refundPrice')->default(0)->comment('退款金额');
            $table->integer('otherPrice')->default(0)->comment('其他支出');
            $table->unsignedInteger('activityExpenditure')->default(0)->comment('商家活动支出');
            $table->unsignedInteger('redPrice')->default(0)->comment('所有优惠金额');
            $table->string('redLists', 255)->default('')->comment('活动列表json存');
            $table->unsignedInteger('fullReductionPrice')->default(0)->comment('满减金额');
            $table->unsignedInteger('expectIncome')->default(0)->comment('预计收入');

            $table->tinyInteger('expectFast')->default(0)->comment('是否期望立即送达 0否 1是');
            $table->unsignedInteger('expectTime')->default(0)->comment('期望送达时间');
            $table->unsignedInteger('acceptTime')->default(0)->comment('接单时间');
            $table->unsignedInteger('completeTime')->default(0)->comment('完成时间');
            $table->string('senderName', 32)->default('')->comment('配送员姓名');
            $table->string('senderPhone', 32)->default('')->comment('配送员电话');
            $table->string('receiverName', 32)->default('')->comment('收货人姓名');
            $table->string('receiverPhone', 32)->default('')->comment('收货人真实电话，带*');
            $table->string('receiverPrivacyPhones', 64)->default('')->comment('收货人隐私号，多个json存');
            $table->string('receiverAddress', 64)->default('')->comment('收货地址');
            $table->string('acceptAdminName', 16)->default('')->comment('接单的收银员姓名');
            $table->string('remark', 64)->default('')->comment('备注');
            $table->string('refundReason', 128)->default('')->comment('取消订单的理由');
            $table->string('refundPic', 256)->default('')->comment('取消订单的图片');
            $table->string('rejectReason', 128)->default('')->comment('商家拒绝退款的理由');

            $table->unsignedInteger('createdAt')->default(0)->comment('创建时间');
            $table->unsignedInteger('updatedAt')->default(0)->comment('更新时间');

            $table->index('shopId', 'shopId_idx');
            $table->index('createdAt', 'createdAt_idx');
            $table->index('payPrice', 'payPrice_idx');
            $table->index('serialNum', 'serialNum_idx');
            $table->index('orderStatus', 'orderStatus_idx');

            $table->collation = 'utf8mb4_general_ci';
            $table->comment   = '外卖订单表'.$this->name;
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->create('waimai_orders' . $this->name, $cFunc);
        }

        $this->name = '';

        // 融合库
        // Schema::connection('mysqlWaiMaiAll')->create('waimai_orders', $cFunc);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        for ($i = 0; $i <= 99; $i++) {
            $name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlWaiMai")->dropIfExists('waimai_orders' . $name);
        }

//        Schema::connection('mysqlWaiMaiAll')->dropIfExists('waimai_orders');
    }
}
